// miniprogram/pages/home/home.js
import { formatTimeToDate } from "../../utils/util";
const db = wx.cloud.database(); // 初始化数据库
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    date: "",
    currentDate: new Date().getTime(),
    minDate: new Date("1900-01-01").getTime(),
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      } else if (type === "month") {
        return `${value}月`;
      }
      return value;
    },
    show: false,
    radio: "1",
    openid: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getOpenId();
  },

  onChange: function(e) {
    this.setData({
      name: e.detail
    });
  },
  onChange2(e) {
    console.log(e);
    this.setData({
      radio: e.detail
    });
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail
    });
  },

  onStartTime() {
    this.setData({
      show: true
    });
  },

  confirm(val) {
    this.setData({
      date: formatTimeToDate(val.detail),
      show: false
    });
  },

  cancel() {
    this.setData({
      show: false
    });
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail
    });
  },

  search(cb) {
    let that = this;
    db.collection("user")
      .where({
        _openid: that.data.openid,
        name: that.data.name,
        birthday: that.data.date,
        sex: this.data.radio
      })
      .get()
      .then(res => {
        console.log(res);
        if (res.data.length > 0) {
          return;
        } else {
          cb();
        }
      })
      .catch(err => {
        console.log(err);
      });
  },

  insert() {
    let that = this;
    db.collection("user")
      .add({
        data: {
          name: that.data.name,
          birthday: that.data.date,
          sex: this.data.radio
        }
      })
      .then(res => {
        console.log(res);
      })
      .catch(err => {
        console.log(err);
      });
  },

  getOpenId() {
    let that = this;
    wx.cloud
      .callFunction({
        name: "login"
      })
      .then(res => {
        that.data.openid = res.result.openid;
      })
      .catch(err => {
        console.log(err);
      });
  },

  submit() {
    if (!this.data.name) {
      wx.showToast({
        icon: "none",
        title: "姓名不能为空",
        duration: 3000
      });
      return;
    }
    if (!this.data.date) {
      wx.showToast({
        icon: "none",
        title: "生日不能为空",
        duration: 3000
      });
      return;
    }
    this.search(this.insert);
    wx.navigateTo({
      url: `../life-code/life-code?name=${this.data.name}&date=${this.data.date}`
    });
  },

  goRecord() {
    wx.navigateTo({
      url: `../record/record?openid=${this.data.openid}`
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
